package chapter15;

import java.util.Arrays;

/**
 * Created by lzy
 * Date: 2018/5/11
 */
public class GenericArray<T> {
    private T[] array;

    @SuppressWarnings("unchecked")
    public GenericArray(int sz) {
        array = (T[]) new Object[sz];
    }

    public void put(int index, T item) {
        array[index] = item;
    }

    public T get(int index) {
        return array[index];
    }

    public T[] rep(){
        return array;
    }

    public static void main(String[] args) {
        GenericArray<Integer> gai = new GenericArray<>(10);
        for (int i = 0; i < 10; i++) {
            gai.put(i,i);
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(gai.get(i)+"");
        }
        System.out.println();
        try {
            Object[] ia = gai.array;
            Integer[] aa = (Integer[]) ia; //java.lang.ClassCastException
            Arrays.stream(aa).forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
